import Vue from 'vue'
import VueRouter from 'vue-router'


Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect:'/login',
    component: () => import('@/components/Login') //路由懒加载
    //component: resolve => require(['@/components/Home'], resolve) //异步加载
},
{
    path: '/login',
    name:'Login',
    component: () => import('@/components/Login') //路由懒加载
    //component: resolve => require(['@/components/Home'], resolve) //异步加载
},

  {
    path: '/main',
    name: 'mian',
    
    component: () => import( '../views/Main.vue'),
    redirect:'/main/productClass',
    children:[
      {
      path:'index',
      component:()=>import('../views/Index.vue')
    },
    {
      path:'productClass',
      component:()=>import('../views/ProductClass.vue')
    },
    {
      path:'product',
      component:()=>import('../views/Product.vue')
    },
    {
      path:'shopOrder',
      component:()=>import('../views/ShopOrder.vue')
    },
    {
      path:'recycleOrder',
      component:()=>import('../views/RecycleOrder.vue')
    },
    {
      path:'location',
      component:()=>import('../views/Location.vue')
    },
    {
      path:'swiper',
      component:()=>import('../views/Swiper.vue')
    },
    {
      path:'news',
      component:()=>import('../views/News.vue')
    },
    {
      path:'after',
      component:()=>import('../views/After.vue')
    },
    {
      path:'user',
      component:()=>import('../views/User.vue')
    }
  ]
  }
  
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, from, next) => {
  // 1. 判断是不是登录页面
  // 是登录页面
  if(to.path === '/login') {
    next()
  } else {
    // 不是登录页面
    // 2. 判断 是否登录过
    let token = sessionStorage.getItem('token')
    token ? next() : next('/login')
  }
})

export default router
